package com.ui;

import com.pojo.Contact;
import com.ui.builder.ButtonBuilder;
import com.ui.builder.FontBuilder;
import com.ui.contact.*;
import com.ui.group.AddGroup;
import com.ui.group.DeleteGroup;
import com.ui.group.ModifyGroup;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Home extends JFrame implements ActionListener {
    private final JPanel contactButtonPanel = new JPanel();
    private JScrollPane scrollPane;
    private final JPanel groupButtonPanel = new JPanel();
    private final JTable table = new JTable();
    private final JButton allContact = ButtonBuilder.makeButton("所有联系人");
    private final JButton modifyContact = ButtonBuilder.makeButton("修改联系人");
    private final JButton addContact = ButtonBuilder.makeButton("添加联系人");
    private final JButton deleteContact = ButtonBuilder.makeButton("删除联系人");
    private final JButton findContact = ButtonBuilder.makeButton("查找联系人");
    private final JButton addGroup = ButtonBuilder.makeButton("添加分组");
    private final JButton modifyGroup = ButtonBuilder.makeButton("修改分组");
    private final JButton deleteGroup = ButtonBuilder.makeButton("删除分组");
    private final String[] columnNames = {"姓名", "电话", "性别", "住址", "邮箱", "群组", "星标"};

    public Home() {
        super("通讯录系统");
        addButtonListeners();
        addContactButtonPanel();
        addScrollTable();
        addGroupButtonPanel();
        setFrame();
    }

    private void addButtonListeners() {
        allContact.addActionListener(this);
        modifyContact.addActionListener(this);
        addContact.addActionListener(this);
        deleteContact.addActionListener(this);
        findContact.addActionListener(this);
        addGroup.addActionListener(this);
        modifyGroup.addActionListener(this);
        deleteGroup.addActionListener(this);
    }

    private void addContactButtonPanel() {
        contactButtonPanel.setLayout(new GridLayout(1, 5, 30, 0));
        contactButtonPanel.add(allContact);
        contactButtonPanel.add(modifyContact);
        contactButtonPanel.add(addContact);
        contactButtonPanel.add(deleteContact);
        contactButtonPanel.add(findContact);
    }

    private void addScrollTable() {
        DefaultTableModel model = new DefaultTableModel(null, columnNames);
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JLabel.CENTER);

        table.setModel(model);
        table.setFont(FontBuilder.makeInputFont());
        table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        table.setRowHeight(25);
        table.setDefaultRenderer(Object.class, renderer);

        scrollPane = new JScrollPane(table) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1100, 600);
            }
        };
        scrollPane.setViewportView(table);
    }

    private void addGroupButtonPanel() {
        groupButtonPanel.setLayout(new GridLayout(1,3,30,0));
        groupButtonPanel.add(addGroup);
        groupButtonPanel.add(modifyGroup);
        groupButtonPanel.add(deleteGroup);
    }

    private void setFrame() {
        this.setLayout(new FlowLayout());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(1200, 800);
        this.setLocationRelativeTo(null);
        this.add(contactButtonPanel);
        this.add(scrollPane);
        this.add(groupButtonPanel);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object button = e.getSource();
        if(button == allContact) {
            doAllContact();
        }
        else if(button == addContact) {
            doAddContact();
        }
        else if(button == deleteContact) {
            doDeleteContact();
        }
        else if(button == modifyContact) {
            doModifyContact();
        }
        else if(button == findContact) {
            doFindContact();
        }
        else if(button == addGroup) {
            doAddGroup();
        }
        else if(button == modifyGroup) {
            doModifyGroup();
        }
        else if(button == deleteGroup) {
            doDeleteGroup();
        }
    }

    private void doAllContact() {
        new AllContact(table);
    }

    private void doAddContact() {
        new AddContact();
    }

    private void doDeleteContact() {
        new DeleteContact(getSelectedContact());
    }

    private void doModifyContact() {
        new ModifyContact(getSelectedContact());
    }

    private void doFindContact() {
        new QueryContact(table);
    }

    private void doAddGroup() {
        new AddGroup();
    }

    private void doModifyGroup() {
        new ModifyGroup();
    }

    private void doDeleteGroup() {
        new DeleteGroup();
    }

    /**
     * 获取列表中选定的联系人
     * @return 选中的Contact对象或null
     */
    private Contact getSelectedContact() {
        int row = table.getSelectedRow();
//        System.out.println(row);
        if(row == -1) {
            JOptionPane.showMessageDialog(null, "您还未选中联系人!");
            System.out.println("未选中联系人");
            return null;
        }
        else {
            DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
            Contact chosenContact = new Contact("", "", "", "", "", "", false);
            chosenContact.setName((String) tableModel.getValueAt(row, 0));
            chosenContact.setPhone((String) tableModel.getValueAt(row, 1));
            chosenContact.setGender((String) tableModel.getValueAt(row, 2));
            chosenContact.setAddress((String) tableModel.getValueAt(row, 3));
            chosenContact.setEmail((String) tableModel.getValueAt(row, 4));
            chosenContact.setGroup((String) tableModel.getValueAt(row, 5));
            chosenContact.setStar(tableModel.getValueAt(row, 6).equals("是"));
            return chosenContact;
        }
    }
}
